import { ajax, tools } from './ajax.js';

let selbj = document.getElementById('selbj');
//#region 班级的部分
function querybj() {
  ajax.post('/linkinfo/queryAllClass', {}, (data) => {
    if (!data.success) {
      alert(data.message);
      return;
    }
    showbj(data.list);
  });
}

function showbj(list) {
  for (let i = 0; i < list.length; i++) {
    let info = list[i];
    let op = document.createElement('option');
    op.setAttribute('value', info.cid);
    op.append(info.cname);
    selbj.append(op);
  }
}

//设置选中索引值
selbj.SelectedIndex = 0;
queryxs();
selbj.addEventListener('change', queryxs);
querybj();
//#endregion

let tbData = document.getElementById('tbData');
//#region 学生的部分

function queryxs() {
  ajax.post(
    '/linkinfo/queryStudentByClass',
    {
      cid: selbj.value,
    },
    (data) => {
      if (!data.success) {
        alert(data.message);
        return;
      }
      showxs(data.list);
    }
  );
}

function showxs(list) {
  tbData.innerHTML = '';
  //生成表格数据
  for (let i = 0; i < list.length; i++) {
    let info = list[i];
    //创建tr，表示一行数据
    let tr = document.createElement('tr');
    //创建td表示员一个数据单元格
    let td;
    td = document.createElement('td');
    td.append(info.cid);
    tr.append(td);

    td = document.createElement('td');
    td.append(info.sname);
    tr.append(td);

    td = document.createElement('td');
    td.append(info.phone);
    tr.append(td);

    td = document.createElement('td');
    td.append(info.address);
    tr.append(td);

    td = document.createElement('td');
    td.append(info.qq);
    tr.append(td);

    td = document.createElement('td');
    td.append(info.wechat);
    tr.append(td);

    td = document.createElement('td');
    td.append(tools.formatDate(info.lastupdate));
    tr.append(td);
    tbData.append(tr);
  }
}
//#endregion
